//
//  UserPointsUTableViewController.m
//  OrderMeal
//
//  Created by 江苏宏创 on 14-7-22.
//  Copyright (c) 2014年 qkong. All rights reserved.
//

#import "UserPointsUTableViewController.h"
#import "PointsTableViewCell.h"
#import "SendHttpOperation.h"
#import "defineFile.h"
@interface UserPointsUTableViewController ()<PassPointsProtocol>{
    NSMutableArray *tableData;
}
@end

@implementation UserPointsUTableViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //下拉刷新
    UIRefreshControl *refreshControl = [[UIRefreshControl alloc]init];
    [refreshControl addTarget:self action:@selector(refreshTableView) forControlEvents:UIControlEventValueChanged];
    self.refreshControl = refreshControl;
    [self.tableView setContentOffset:CGPointMake(0, -85) animated:YES];
    [self.refreshControl beginRefreshing];
    
    tableData = [[NSMutableArray alloc]init];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    [SendHttpOperation sharedHttpOperation].passPointsDelegate = self;
    NSString *customerId = [userDefaults objectForKey:@"customerId"];
    
    [[SendHttpOperation sharedHttpOperation] getPointsWithcustomerId:customerId UITableViewController:self];
    
    //去除底部多余cell
    UIView *clearView = [UIView new];
    [clearView setBackgroundColor:[UIColor clearColor]];
    [self.tableView setTableFooterView:clearView];
}

-(void)refreshTableView{
    NSString *customerId = [userDefaults objectForKey:@"customerId"];
    [[SendHttpOperation sharedHttpOperation] getPointsWithcustomerId:customerId UITableViewController:self];
}


-(void)passPointsData:(NSMutableArray *)pointsData{
    dispatch_async(dispatch_get_main_queue(), ^{
        tableData = pointsData;
        [self.tableView reloadData];
    });
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return tableData.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    PointsTableViewCell *cell = (PointsTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"PointsCell"];
    cell.points.text = [NSString stringWithFormat:@"%@积分",tableData[indexPath.row][@"num"]];
    cell.time.text = tableData[indexPath.row][@"createTime"];
    cell.pointMsg.text = tableData[indexPath.row][@"description"];
    return cell;
}

@end
